﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using XKPlatform.Common;
using XKPlatform.Common.Enums;
using XKPlatform.DataModels.Dao.BaseEntity;
using XKPlatform.DataModels.ReturnModel;
using XKPlatform.IService;

namespace XKPlatform.Controllers.SysController
{
    [Route("[controller]/[action]")]
    public class SysApiController : BaseController
    {
        private readonly ISysApiService _iservice;
        public SysApiController(ISysApiService iservice, IHttpContextAccessor httpContext) : base(httpContext)
        {
            _iservice = iservice;
        }
        /// <summary>
        /// 查询获取数据
        /// </summary>
        /// <param name="searchModel">查询参数</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetList([FromBody] SysApiSearchParams search)
        {
            PageInfo pageinfo = new PageInfo();
            pageinfo.PageIndex = search.Page;
            pageinfo.PageSize = search.Limit;
            pageinfo.IsPaging = search.IsPage == null ? true : Convert.ToBoolean(search.IsPage);
            long count = 0;

            List<SortInfo<sysApiModel, object>> sorLst = new List<SortInfo<sysApiModel, object>>();
            SortInfo<sysApiModel, object> sorItem = new SortInfo<sysApiModel, object>();
            sorItem.SortMethods = SortEnum.Desc;
            //#注：默认按创建时间倒叙，如果有自定义排序规则修改此处
            sorItem.Orderby = o => o.CreatedOn;
            sorLst.Add(sorItem);
            Expression<Func<sysApiModel, bool>> where = o => o.RecordStatus == false;
            //组装查询条件

            if (!string.IsNullOrWhiteSpace(search.PageUrl))
            {
                where = where.And(o => o.PageUrl.Contains(search.PageUrl));
            }
            if (search.IsNeedPermisson != null)
            {
                var per = Convert.ToBoolean(search.IsNeedPermisson);
                where = where.And(o => o.IsNeedPermisson == per);
            }
            if (!string.IsNullOrWhiteSpace(search.ApiSource))
            {
                where = where.And(o => o.ApiSource == search.ApiSource);
            }

            if (!string.IsNullOrWhiteSpace(search.UrlAddr))
            {
                where = where.And(o => o.UrlAddr.Contains(search.UrlAddr));
            }



            var ret = _iservice.GetList(where, sorLst, pageinfo);
            count = ret.count;
            var list = ret.list;

            return Json(new BasePageModel { code = (int)MsgCodeEnum.Success, msg = EnumUtils.GetEnumDesc(MsgCodeEnum.Success), data = list, count = Convert.ToInt32(count) });

        }
        /// <summary>
        /// 新增或修改
        /// </summary>
        /// <param name="save"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveOrEdit([FromBody] sysApiModel save)
        {

            //判断主键为空 则是新增
            if (string.IsNullOrEmpty(save.ID))
            {
                //#补充主键 创建信息 
                save.ID = Guid.NewGuid().ToString();
                save.CreatedBy = userInfo.UserName;
                save.CreatedOn = DateTime.Now.ToString("yyyy-MM-dd:HH:mm:ss");
                save.RecordStatus = false;
                _iservice.AddModel(save);
            }
            else
            {
                save.ModifyBy = userInfo.UserName;
                save.ModifyOn = DateTime.Now.ToString("yyyy-MM-dd:HH:mm:ss");
                //#注：忽略更新的列可以继续在 new{ o.CreatedBy,o.CreatedOn} 中追加
                _iservice.UpdateModel(save, o => o.ID == save.ID, o => new { o.CreatedBy, o.CreatedOn });
            }

            return Json(new BasePageModel { code = (int)MsgCodeEnum.Success, msg = EnumUtils.GetEnumDesc(MsgCodeEnum.Success) });

        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DelAll([FromBody] List<string> ids)
        {
            var isDelete = false;

            isDelete = _iservice.Delete(o => ids.Contains(o.ID));

            if (isDelete)
            {
                return Json(new BasePageModel { code = (int)MsgCodeEnum.Success, msg = EnumUtils.GetEnumDesc(MsgCodeEnum.Success) });
            }
            else
            {
                return Json(new BasePageModel { code = (int)MsgCodeEnum.Failed, msg = EnumUtils.GetEnumDesc(MsgCodeEnum.Failed) });

            }
        }
    }
}
